#include "STreeComboWidget.h"

#include <core/StyleMgr.h>

#include <QMouseEvent>

#include "SFilterButton.h"

STreeComboWidget::STreeComboWidget(QWidget* pPanent, bool showInvert,
                                   bool showFilterIndex, int itemHeight,
                                   int dropHeight)
    : QListWidget(pPanent), m_showFilterIndex(showFilterIndex) {
  QString style =
      QString(
          "QListWidget::item:hover{background:#%3;}"
          "QListWidget{color:#%2;font:13px Microsoft YaHei;font-weight:bold;}"
          "QListWidget{background:#%3;}"
          "QListWidget "
          "QAbstractItemView::item{padding-left:4px;color:#%1;height:%5px;"
          "background-color:#%2;}"
          "QListWidget QAbstractItemView::item:hover{background-color:#%4;}"
          "QListWidget QAbstractItemView::item:selected{background-color:#%2;}")
          .arg(StyleMgr::instance().color("Hover").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("TextNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("BackNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("MenuTitleBack").rgb(), 0, 16)
          .arg(itemHeight);
  setStyleSheet(style);

  QListWidgetItem* wItem = new QListWidgetItem(this);
  QWidget* widget = new QWidget(this);  // 新建一个QWidget窗口
  QGridLayout* glayout = new QGridLayout;
  glayout->setContentsMargins(0, 0, 4, 4);

  SFilterButton* tabFilterIndex = nullptr;
  if (m_showFilterIndex) {
    tabFilterIndex = new SFilterButton(this);
    tabFilterIndex->setName("");
    tabFilterIndex->setNameWidth(0);
    tabFilterIndex->setTabFixedWidth(24);
    tabFilterIndex->setMultiSel(false);

    for (char i = 'A'; i <= 'Z'; ++i) {
      tabFilterIndex->addTab(QString(i));
    }

    tabFilterIndex->selectTab(0);
    tabFilterIndex->setFixedHeight(80);
    connect(tabFilterIndex, &SFilterButton::tabSelected, this,
            &STreeComboWidget::onFilterIndexClick);
  }

  m_tree = new STreeWidget();
  m_tree->setFocusPolicy(Qt::NoFocus);

  // 	m_tree->startSetting();
  // 	for (int i = 1; i <= 2; i++)
  // 	{
  // 		QTreeWidgetItem* item = new QTreeWidgetItem(m_tree, {
  // QString::number(i) }); 		Qt::ItemFlags flg = item->flags(); 		flg |=
  // Qt::ItemIsUserCheckable; 		flg &= ~Qt::ItemIsSelectable; 		item->setFlags(flg);
  // 		item->setCheckState(0, Qt::Unchecked);
  // 		for (int j = 1; j <= 3; j++)
  // 		{
  // 			QTreeWidgetItem* sub = new QTreeWidgetItem(item, {
  // QString("%1.%2").arg(i).arg(j) }); 			flg = sub->flags(); 			flg |=
  // Qt::ItemIsUserCheckable; 			sub->setFlags(flg); 			sub->setCheckState(0,
  // Qt::Unchecked); 			for (int k = 1; k <= 4; k++)
  // 			{
  // 				QTreeWidgetItem* exsub = new
  // QTreeWidgetItem(sub, { QString("%1.%2.%3").arg(i).arg(j).arg(k) }); 				flg =
  // exsub->flags(); 				flg |= Qt::ItemIsUserCheckable; 				exsub->setFlags(flg);
  // 				exsub->setCheckState(0, Qt::Unchecked);
  // 			}
  // 		}
  // 	}
  // 	m_tree->endSetting();
  m_tree->setFixedHeight(dropHeight);
  if (m_showFilterIndex) {
    glayout->addWidget(tabFilterIndex, 0, 0);
    glayout->addWidget(m_tree, 1, 0);
  } else {
    glayout->addWidget(m_tree, 0, 0);
  }

  if (showInvert) {
    m_btnInvert = new QPushButton(tr("反选"));
    m_btnInvert->setFixedSize(80, 24);
    glayout->addWidget(m_btnInvert, 1, 0, Qt::AlignVCenter | Qt::AlignCenter);
    connect(m_btnInvert, &QPushButton::clicked, this,
            &STreeComboWidget::onBtnClick);
  }

  widget->setLayout(glayout);
  /******************end****************/

  wItem->setFlags(wItem->flags() & ~Qt::ItemIsSelectable);
  wItem->setSizeHint(QSize(40, dropHeight));
  this->setItemWidget(wItem, widget);  // 将WContainer赋予WContainerItem
  connect(m_tree, &STreeWidget::itemCheckChanged, this,
          &STreeComboWidget::onItemChange);
}

void STreeComboWidget::setTreeData(STreeData& treeData) {
  if (!m_tree) return;
  m_tree->setTreeData(treeData);
}

void STreeComboWidget::clearTreeData() {
  if (!m_tree) return;
  m_tree->clear();
}

void STreeComboWidget::clearSelect() {
  if (!m_tree) return;
  m_tree->clearSelect();
}

void STreeComboWidget::setAutoDisappear() {
  m_autoDisappear = true;

  installEventFilter(this);
}

void STreeComboWidget::showToolTip(int limitWidth) {
  if (!m_tree) return;
  m_tree->showToolTip(limitWidth);
}

void STreeComboWidget::onBtnClick() {
  if (!m_tree) return;
  m_tree->invertCheck();
}

void STreeComboWidget::onItemChange() { emit itemSelectChanged(); }

void STreeComboWidget::onFilterIndexClick(int index) {
  emit filterIndexSelectChanged(index);
}

bool STreeComboWidget::getSelectItemText(QStringList& lstText) {
  if (!m_tree) return false;
  return m_tree->getSelectItemText(lstText);
}

const QStringList& STreeComboWidget::getSelectItemData() {
  assert(m_tree);
  return m_tree->getSelectItemData();
}

bool STreeComboWidget::eventFilter(QObject* watched, QEvent* event) {
  if (!m_autoDisappear) return QListWidget::eventFilter(watched, event);
  if (!isVisible()) return false;

  switch (event->type()) {
    case QEvent::Hide:
    case QEvent::WindowDeactivate: {
      hide();
    } break;
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    case QEvent::MouseButtonDblClick: {
      QMouseEvent* me = dynamic_cast<QMouseEvent*>(event);
      QPoint pt = me->globalPos();
      pt = mapFromGlobal(pt);
      if (!rect().contains(pt)) {
        hide();
      }
    } break;
    default:
      break;
  }

  return QListWidget::eventFilter(watched, event);
}